package com.tubruk.adapter;

import java.util.List;

import com.tubruk.object.IOption;
import com.tubruk.object.TrackSummary;
import com.tubruk.trackme.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class TrackSummaryAdapter extends ArrayAdapter<TrackSummary> {

	private List<TrackSummary> items;
	
	public TrackSummaryAdapter(Context context, 
			int textViewResourceId, List<TrackSummary> items) {
		super(context, textViewResourceId, items);

		this.items = items;
	}

	
	@Override
    public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		if (v == null) {
            LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.history_row, null);
        }
		
		TrackSummary sum = items.get(position);
	
		TextView tv = (TextView) v.findViewById(R.id.title);
		String temp = sum.getTitle();
		if (temp == null) {
			tv.setText("");
		}else {
			tv.setText(temp);
		}
		
		tv = (TextView) v.findViewById(R.id.description);
		StringBuffer sb = new StringBuffer("Min: ").append(sum.getMinSpeedStr())
								.append(", Avg: ").append(sum.getAvgSpeedStr())
								.append(", Max: ").append(sum.getMaxSpeedStr());
		tv.setText(sb.toString());
		
		tv = (TextView) v.findViewById(R.id.duration);
		temp = sum.getTimeSpentStr();
		if (temp == null){
			tv.setText("");
		}else {
			tv.setText(temp);
		}
		
		return v;
	}
}
